import java.util.Arrays;

public class Test {

    public static String myToString(int[] array){
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret+= array[i];
            if(i != array.length-1){
                ret += ",";
            }
        }
        ret += "]";

        return ret;
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        String str = myToString(array);
        System.out.println(str);
    }

    public static int[] init2(){
        int a = 10;
        int b = 20;
        int[] array = new int[]{a,b};
        return array;
    }

    public static int[] init(){
        return new int[]{1,2};
    }

    public static void main12(String[] args) {
        System.out.println(Arrays.toString(init()));
    }

    public static void main11(String[] args) {
        int[] array = init();
        String str = Arrays.toString(array);
        System.out.println(str);
    }

    public static void swap(int[] array){
        int tmp = array[0];
        array[0] = array[1];
        array[1] = tmp;
    }

    public static void main10(String[] args) {
        int[] array = {1,2};
        System.out.println(array[0] + " " + array[1]);
        swap(array);
        System.out.println(array[0] + " " + array[1]);
    }

    public static void test2(int[] array){
        array = new int[] {100,200,300,400,500};
        array[0] = 1000000;
    }

    public static void test1(int[] array){
        array[0] = 99;
    }

    public static void main9(String[] args) {
        int[] array = {1,2,3,4,5};
        // test1(array);
        test2(array);
        printArray(array);
    }

    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void function(int x){
        x = 100;
        System.out.println("x = " + x);
    }

    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5};
        printArray(array);

        int a = 10;
        function(a);
        System.out.println("a = " + a);
    }

    public static void main7(String[] args) {
        int[] array = null; // 不指向任何对象
        System.out.println(array[2]); // 对空引用使用时,会抛出空指针异常

    }

    public static void func(){
        int[] array1 = new int[3];
        array1[0] = 10;
        array1[1] = 20;
        array1[2] = 30;

        int[] array2 = new int[]{1,2,3,4,5};
        array2[0] = 100;
        array2[1] = 200;

        array1 = array2;
        array1[2] = 300;
        array1[3] = 400;
        array1[4] = 500;
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
    }



    public static void main6(String[] args) {
        int a = 10;
        int b = 20;
        int[] array = {1,2,3,4,5};
        System.out.println(array);

        int c = 0;
        System.out.println(c);

        int[] array2 = null;
        System.out.println(array2);
    }

    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5};
        // System.out.println(array[3]); // 越界异常

        /*for (int i = 0;  i < 5;  i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();*/

        int len = array.length;
        // System.out.println(len);
        for (int i = 0; i < len; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();

        System.out.println("----------------------------");
        System.out.println("for each");

        for (int x: array) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("-----------借助工具-----------");

        /*String str = Arrays.toString(array);
        System.out.println(str);*/

        System.out.println(Arrays.toString(array));


    }

    public static void main4(String[] args) {
        int[] array;
        array = new int[5];

        int[] array2;
        array2 = new int[]{1,2,3,4,5};

        // 这种方式只有在定义数组的时候可以直接用花括号进行初始化,一旦分开定义和赋值,编译器会报错
        // 解决办法 array3 = new int[]{1,2,3,4,5};
        /*int[] array3;
        array3 = {1, 2, 3, 4, 5, 6};*/ // error
    }

    public static void main3(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        int[] arr = {4,5,6,7,8};
        System.out.println(arr[1]);
    }

    public static void main2(String[] args) {
        int[] array = new int[5];
        array[2] = 100;
        int ret = array[2];
        System.out.println(ret);

    }

    public static void main1(String[] args) {
        int[] array = new int[5]; // 默认赋值0
        double[] array2 = new double[5]; // 默认赋值 0.0
        String[] array3 = new String[5]; // 默认赋值null

    }
}
